package com.taoyibao.hongcanting.ui.mine.delegate;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;
import com.taoyibao.hongcanting.widget.PasswordInputView;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 17:07
 */
public class SetPayPwdStepTwoDelegate extends BackToolbarDelegate implements PasswordInputView.OnFinishListener, TextWatcher {
    /**
     * 第一次输入的密码
     */
    public String mPwdOne;
    /**
     * 第二次输入的密码
     */
    public String mPwdTwo;

    private PasswordInputView mPasswordInputView;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_set_pay_pwd_step2;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("设置支付密码");
        mPasswordInputView = get(R.id.piv_setPayPwd2_input);
        mPasswordInputView.setOnFinishListener(this);
//        mPasswordInputView.addTextChangedListener(this);
    }

    /**
     * 是否可以调用接口提交密码
     *
     * @return
     */
    public boolean canSubmitPwd() {
        if (TextUtils.isEmpty(mPwdOne) || mPwdOne.length() < 6) {//第一次密码不合格
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputPayPwd));
        } else if (mPwdOne.length() == 6 && TextUtils.isEmpty(mPwdTwo)) {//修改UI变成再次输入
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintAgainInputPayPwd));
            setUITwo();
        } else if (TextUtils.isEmpty(mPwdTwo) || mPwdTwo.length() < 6) {//第二次输入的不正确
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputPayPwd));
        } else if (!TextUtils.equals(mPwdOne, mPwdTwo)) {//两次输入的不同  提示输入的不同 重新设置  清空之前记录的数据
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputPayPwdDifferent));
            setUIOne();
            mPwdOne = "";
            mPwdTwo = "";
        } else {
            return true;
        }

        return false;
    }

    /**
     * 设置再次输入的UI
     */
    private void setUITwo() {
        ((TextView) get(R.id.tv_setPayPwd2_topHint)).setText(CodeUtils.getIdString(R.string.string_pleaseAgainInputPayPwd));
        mPasswordInputView.setText("");
        mPwdTwo = "";
    }

    /**
     * 设置第一次输入的UI
     */
    private void setUIOne() {
        ((TextView) get(R.id.tv_setPayPwd2_topHint)).setText(CodeUtils.getIdString(R.string.string_pleaseInputPayPwd));
        mPwdOne = "";
        mPasswordInputView.setText("");
    }


    /**
     * 获取输入的密码
     *
     * @return
     */
    public String getInputPwd() {
        return mPasswordInputView.getOriginText();
    }

    @Override
    public void setOnPasswordFinished() {
        if (TextUtils.isEmpty(mPwdOne) || mPwdOne.length() < 6) {
            mPwdOne = mPasswordInputView.getOriginText();
        } else {
            mPwdTwo = mPasswordInputView.getOriginText();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    /**
     * @param s
     * @param start
     * @param before
     * @param count
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(mPwdOne) || mPwdOne.length() < 6) {
            mPwdOne = mPasswordInputView.getOriginText();
        } else {
            mPwdTwo = mPasswordInputView.getOriginText();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
